﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Maxcoderz
{
    public class Block
    {
        private Vector2 _position;
        private float _rotation = 0f;
        private float _zoom = 1f;
        private Sprite _sprite;

        #region Properties
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }
        public float Rotation
        {
            get { return _rotation; }
            set 
            {
                if (_rotation > MathHelper.ToRadians(360))
                {
                    _rotation = 0;
                }
                else if (_rotation < 0)
                {
                    _rotation = MathHelper.ToRadians(360);
                }
                else
                {
                    _rotation = value;
                }
            }
        }
        public float Zoom
        {
            get { return _zoom; }
            set
            {
                if (value > 0) 
                { 
                    _zoom = value; 
                }
            }
        }
        public Sprite Sprite
        {
            get { return _sprite; }
            set { _sprite = value; }
        }
        #endregion

        public Block(Sprite sprite)
        {
            _sprite = sprite;
        }

        public Block(Sprite sprite, Vector2 position) : this(sprite)
        {
            _position = position;
        }

        public void Draw()
        { 
            Vector2 turningPoint = new Vector2();
            turningPoint.X = _sprite.SourceRectangle.Width / 2;
            turningPoint.Y = _sprite.SourceRectangle.Height / 2;

            // Draw the current selected sprite at the position of the mouse.
            Global.SpriteBatch.Draw(Global.GlobalTexture, GetDrawRectangle(), _sprite.SourceRectangle, Color.White, _rotation, turningPoint, SpriteEffects.None, 0);             
        }

        private Rectangle GetDrawRectangle()
        {
            return new Rectangle(
            (int)(_position.X + ((_sprite.SourceRectangle.Width * Zoom) / 2)), //X
            (int)(_position.Y + ((_sprite.SourceRectangle.Height * Zoom) / 2)), //Y
            (int)(_sprite.SourceRectangle.Width * _zoom), //Width
            (int)(_sprite.SourceRectangle.Height * _zoom)); //Height   
        }

        public Rectangle GetDestinationRectangle()
        {
            float width = _sprite.SourceRectangle.Width * _zoom;
            float height = _sprite.SourceRectangle.Height * _zoom;

            float x_sin;
            float y_sin;
            if (_rotation <= MathHelper.ToRadians(180))
            {
                x_sin = (float)(Math.Sin(_rotation) * width);
                y_sin = (float)(Math.Sin(_rotation) * height);
            }
            else
            {
                x_sin = (float)(-Math.Sin(_rotation) * width);
                y_sin = (float)(-Math.Sin(_rotation) * height);
            }
       

            float rect_height = (float)(x_sin + System.Math.Sqrt((height * height) - (y_sin * y_sin)));
            float rect_width = (float)(y_sin + System.Math.Sqrt((width * width) - (x_sin * x_sin)));

            return new Rectangle(
            (int)(_position.X - ((rect_width - width)/2)),
            (int)(_position.Y - ((rect_height - height)/2)),
            (int)rect_width,
            (int)rect_height);
        }
    }
}
